package com.ziq.baselib.business.post

import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.blankj.utilcode.util.ScreenUtils
import com.ziq.base.base.BaseActivity
import com.ziq.base.extension.BindingViewHolder
import com.ziq.base.extension.binding
import com.ziq.base.transition.ShareElementInfo
import com.ziq.base.transition.ShareElementInfoDataUpdate
import com.ziq.base.transition.TransitionHelper
import com.ziq.base.transition.custom.ChangeTextTransition
import com.ziq.baselib.R
import com.ziq.baselib.business.post.bean.PostingDetail
import com.ziq.baselib.databinding.ActivityMvvmBinding
import com.ziq.baselib.databinding.ItemPostingBinding
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class PostingListActivity: BaseActivity() {

    private val binding : ActivityMvvmBinding by binding()

    val mViewModel: PostingListViewModel by viewModels()

    var adapter: PostingAdapter? = null
    var itemWidth:Int = 0

    var clickPosition = 0

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        println("PostingListActivity mViewModel ${mViewModel.hashCode()}")

        binding.root
        itemWidth = (ScreenUtils.getScreenWidth() - this.resources.getDimensionPixelSize(R.dimen.dimens_dip_31)) / 2
        binding.recycleView.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        adapter = PostingAdapter(itemWidth)
        adapter?.setHasStableIds(true)
        adapter?.setPostingAdapterAction(object : PostingAdapterAction {
            override fun itemClick(item: PostingDetail, holder: BindingViewHolder<ItemPostingBinding>) {
                val intent = Intent(this@PostingListActivity, PostingDetailActivity::class.java)
                intent.putExtra("item", item)
                clickPosition = holder.absoluteAdapterPosition
                TransitionHelper.startActivity(this@PostingListActivity, intent)
            }
        })

        TransitionHelper.setUpTransition(this,
            shareElementInfoList = {
                listOf(
                    ShareElementInfo<ShareElementInfoDataUpdate>("image", binding.recycleView.layoutManager!!.findViewByPosition(clickPosition)!!.findViewById(R.id.image)),
                    ShareElementInfo("title", binding.recycleView.layoutManager!!.findViewByPosition(clickPosition)!!.findViewById(R.id.title), ChangeTextTransition.TextData::class.java)
                )
            }
        )

        binding.recycleView.adapter = adapter
        mViewModel.listRequestFinished.observe(this) {
            if (it == true) {
                binding.refreshLayout.finishRefresh()
                binding.refreshLayout.finishLoadMore()
            }
        }
        mViewModel.listHasMore.observe(this) {
            if (it == true) {
                binding.refreshLayout.resetNoMoreData()
            } else {
                binding.refreshLayout.setNoMoreData(true)
            }
        }

        mViewModel.list.observe(this) {
            adapter?.setData(it)
        }

        binding.refreshLayout.setOnRefreshListener {
            mViewModel.onRefresh()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            mViewModel.onLoadMore()
        }
        mViewModel.onRefresh()
    }



}